package Library.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Libarary.items.SearchedBook;
import Library.dbcommands.DBConnector;
import Library.dbcommands.IDBCommands;

/**
 * Servlet implementation class SearchSerlvet
 */
@WebServlet("/SearchServlet")
public class SearchSerlvet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchSerlvet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String submitAll = request.getParameter("searh");
		IDBCommands connector = DBConnector.connector();
		
		ArrayList<SearchedBook> allFoundBooks = connector.getBooksWhereTitleIs(submitAll);
		System.out.println(allFoundBooks);
		response.getWriter().println("<!DOCTYPE html><html \"><head><title> Home </title><meta http-equiv=\"Content-Language\" content=\"English\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />	<link rel=\"stylesheet\" type=\"text/css\" href=\"css/style.css\" media=\"screen\" /><link href='http://fonts.googleapis.com/css?family=Roboto+Condensed' rel='stylesheet' type='text/css' /></head><body><div id=\"wrap\">	<div id=\"header\"><div id=\"headerlinks\">	<a href=\"index.html\" title=\"Home\">Home</a><a href=\"Contact.html\" title=\"Contact\">Contact</a><a href=\"LogoutServlet\" title=\"LogOut\">Log out</a></div><h1><a href=\"Index.html\">Library</a></h1></div>	<div id=\"sidebar\"><h2>Your features:</h2><div class=\"box\"><ul><li><a href=\"Search.html\">Search for book</a></li><li><a href=\"#\">Which books I have taken</a></li></ul></div></div><div id=\"content\"><table border=\"1\" style=\"width:100%\"><caption>Sample books from our library</caption><tr><th>Title</th> <th>Author</th> <th>Status</th></tr>");

		for (SearchedBook searchedBook : allFoundBooks) {
			response.getWriter().println("<tr><td>"+searchedBook.getTitle()+"</td> <td>"+searchedBook.getAuthorFirstName()+"</td> <td>"+searchedBook.getAuthorLastName()+"</td></tr>");
		}
		response.getWriter().println("</table></body></html>");
	}
}
